﻿
namespace Microsoft.Dynamics.AX.Framework.TeamFoundationServerFacade
{
    using System;
    using System.Collections.Generic;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;

    /// <summary>
    /// A proxy class for WorkItemStore
    /// </summary>
    public class WorkItemStoreProxy
    {
        private ConnectionManagerProxy connectionProxy;
        private Project project;

        internal WorkItemStore WorkItemStore
        {
            get;
            private set;
        }
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connectionProxy"></param>
        public WorkItemStoreProxy(ConnectionManagerProxy connectionProxy)
        {
            this.connectionProxy = connectionProxy;
            this.WorkItemStore = new WorkItemStore(this.connectionProxy.TeamProjectCollection);
            this.project = this.WorkItemStore.Projects[connectionProxy.ProjectName];
        }

        /// <summary>
        /// Gets a work item
        /// </summary>
        /// <param name="workItemId">Work item ID</param>
        /// <returns></returns>
        public WorkItemProxy GetWorkItem(int workItemId)
        {
            WorkItem workItem = null;
            try
            {
                workItem = project.Store.GetWorkItem(workItemId);
            }
            catch (DeniedOrNotExistException)
            {
                return null;
            }

            if (workItem == null)
            {
                return null;
            }

            WorkItemProxy tfsWorkItem = new WorkItemProxy(workItem);
            return tfsWorkItem;
        }

        /// <summary>
        /// Executes a work item query
        /// </summary>
        /// <param name="query">TFS Work Item query</param>
        /// <returns>Resulting work item list</returns>
        public WorkItemProxyCollection ExecuteWorkItemQuery(string query)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException("query");
            
            WorkItemCollection workItemCollection = null;
            Dictionary<string, string> context = new Dictionary<string, string>();
            
            context.Add("project", this.project.Name);
            try
            {
                workItemCollection = this.WorkItemStore.Query(query, context);
            }
            catch (ValidationException e)
            {
                throw new ArgumentException("Validation of query failed. See inner exception for details", e);
            }

            WorkItemProxyCollection result = new WorkItemProxyCollection();
            
            foreach (WorkItem workItem in workItemCollection)
            {
                WorkItemProxy tfsWorkItem = new WorkItemProxy(workItem);
                result.Add(tfsWorkItem);
            }

            return result;
        }
    }
}
